<template>
  <div class="goods-comment">
    <!-- 头部 -->
    <div class="head">
      <div class="data">
        <p>
          <span>{{ commentInfo.salesCount }}</span
          ><span>人购买</span>
        </p>
        <p>
          <span>{{ commentInfo.praisePercent }}</span
          ><span>好评率</span>
        </p>
      </div>
      <div class="tags">
        <div class="dt">大家都在说：</div>
        <div class="dd">
          <a
            v-for="(item, index) in commentInfo.tags"
            :key="index"
            href="javascript:;"
            :class="{ active: activeIndex === index }"
            @click="changeTag(item, index)"
            >{{ item.title }}（{{ item.tagCount }}）</a
          >
        </div>
      </div>
    </div>
    <!-- 排序 -->
    <div class="sort">
      <span>排序：</span>
      <a
        v-for="(item, index) in sortList"
        :key="index"
        href="javascript:;"
        :class="{ active: activeSortIndex === index }"
        @click="changeSort(item, index)"
        >{{ item.name }}</a
      >
    </div>
    <!-- 列表 -->
    <div class="list">
      <div class="item" v-for="item in commentList" :key="item.id">
        <div class="user">
          <img :src="item.member.avatar" alt="" />
          <span>{{ filterName(item.member.nickname) }}</span>
        </div>
        <div class="body">
          <div class="score">
            <i class="iconfont icon-wjx01" v-for="i in item.score" :key="i"></i>
            <i
              class="iconfont icon-wjx02"
              v-for="i in 5 - item.score"
              :key="i"
            ></i>
            <span class="attr" v-for="v in item.orderInfo.specs" :key="v">
              <span>{{ v.name }} : </span>
              <span>{{ v.nameValue }}</span>
            </span>
          </div>
          <div class="text">{{ item.content }}</div>
          <!-- 图片组件 -->
          <GoodsCommentImage
            :pictures="item.pictures"
            v-if="item.pictures.length"
          />
          <div class="time">
            <span>{{ item.createTime }}</span>
            <span class="zan"
              ><i class="iconfont icon-dianzan"></i>{{ item.praiseCount }}</span
            >
          </div>
        </div>
      </div>
    </div>
    <!-- 分页 -->
    <XtxPagination
      :currPage="reqParams.page"
      :total="total"
      @current-change="currentChangeFn"
    />
  </div>
</template>

<script>
import GoodsCommentImage from './goods-comment-image'
import { findGoodsCommentInfo, findGoodsCommentList } from '@/api/product.js'
import { useRoute } from 'vue-router'
import { ref, reactive, watch } from 'vue'
export default {
  name: 'GoodsComment',
  components: {
    GoodsCommentImage
  },
  setup () {
    // 路由参数
    const route = useRoute()
    // 头部数据
    const commentInfo = ref({})
    findGoodsCommentInfo(route.params.id).then(({ result }) => {
      result.tags = [
        { title: '全部评价', tagCount: result.evaluateCount },
        { title: '有图', tagCount: result.hasPictureCount },
        ...result.tags
      ]
      commentInfo.value = result
    })
    // 当前点击项
    const activeIndex = ref(0)

    // 获取评价列表
    const commentList = ref([])
    // 总条数
    const total = ref(0)
    // 查询参数列表
    const reqParams = reactive({
      page: 1,
      pageSize: 10,
      hasPicture: null,
      tag: null,
      sortField: null
    })
    // 处理评论用户名
    const filterName = (name) => {
      return name.slice(0, 1) + '*****' + name.slice(-1)
    }
    // 监听参数变化获取最新列表
    watch(
      reqParams,
      () => {
        findGoodsCommentList(route.params.id, reqParams).then((res) => {
          commentList.value = res.result.items
          // 总条数
          total.value = res.result.counts
        })
      },
      { immediate: true }
    )

    const sortList = [
      { name: '默认', value: null },
      { name: '最新', value: 'createTime' },
      { name: '最热', value: 'praiseCount' }
    ]

    // 标签选择
    const changeTag = (theItem, index) => {
      // 高亮
      activeIndex.value = index
      // 筛选
      if (theItem.title === '全部评价') {
        reqParams.hasPicture = null
        reqParams.tag = null
      } else if (theItem.title === '有图') {
        reqParams.hasPicture = true
        reqParams.tag = null
      } else {
        reqParams.hasPicture = null
        reqParams.tag = theItem.title
      }
      // 重置页码为1
      reqParams.page = 1
    }

    // 排序选择
    const activeSortIndex = ref(0)
    const changeSort = (theItem, index) => {
      // 高亮
      activeSortIndex.value = index
      // 筛选
      if (theItem.name === '默认') {
        reqParams.sortField = null
      } else {
        reqParams.sortField = theItem.value
      }
      // 重置页码为1
      reqParams.page = 1
    }

    // 页码
    const currentChangeFn = (val) => {
      reqParams.page = val
    }

    return {
      commentInfo,
      activeIndex,
      commentList,
      filterName,
      changeTag,
      sortList,
      activeSortIndex,
      changeSort,
      currentChangeFn,
      total,
      reqParams
    }
  }
}
</script>

<style scoped lang="less">
.goods-comment {
  .head {
    display: flex;
    padding: 30px 0;
    .data {
      width: 340px;
      display: flex;
      padding: 20px;
      p {
        flex: 1;
        text-align: center;
        span {
          display: block;
          &:first-child {
            font-size: 32px;
            color: @priceColor;
          }
          &:last-child {
            color: #999;
          }
        }
      }
    }
    .tags {
      flex: 1;
      display: flex;
      border-left: 1px solid #f5f5f5;
      .dt {
        font-weight: bold;
        width: 100px;
        text-align: right;
        line-height: 42px;
      }
      .dd {
        flex: 1;
        display: flex;
        flex-wrap: wrap;
        > a {
          width: 132px;
          height: 42px;
          margin-left: 20px;
          margin-bottom: 20px;
          border-radius: 4px;
          border: 1px solid #e4e4e4;
          background: #f5f5f5;
          color: #999;
          text-align: center;
          line-height: 40px;
          &:hover {
            border-color: @xtxColor;
            background: lighten(@xtxColor, 50%);
            color: @xtxColor;
          }
          &.active {
            border-color: @xtxColor;
            background: @xtxColor;
            color: #fff;
          }
        }
      }
    }
  }
  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;
    > span {
      margin-left: 20px;
    }
    > a {
      margin-left: 30px;
      &.active,
      &:hover {
        color: @xtxColor;
      }
    }
  }
  .list {
    padding: 0 20px;
    .item {
      display: flex;
      padding: 25px 10px;
      border-bottom: 1px solid #f5f5f5;
      .user {
        width: 160px;
        img {
          width: 40px;
          height: 40px;
          border-radius: 50%;
          overflow: hidden;
        }
        span {
          padding-left: 10px;
          color: #666;
        }
      }
      .body {
        flex: 1;
        .score {
          line-height: 40px;
          .iconfont {
            color: #ff9240;
            padding-right: 3px;
          }
          .attr {
            padding-left: 10px;
            color: #666;
          }
        }
      }
      .text {
        color: #666;
        line-height: 24px;
      }
      .time {
        color: #999;
        display: flex;
        justify-content: space-between;
        margin-top: 5px;
      }
    }
  }
}
</style>
